package com.situ.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.situ.web.entity.BanJi;
import com.situ.web.entity.PageBean;
import com.situ.web.entity.Student;
import com.situ.web.entity.User;
import com.situ.web.exception.NameAgainException;
import com.situ.web.service.IStudentService;
import com.situ.web.service.impl.StudentServiceImpl;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class StudentServlet
 */
public class StudentServlet extends HttpServlet {
	private IStudentService studentService =  new StudentServiceImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method = req.getParameter("method");
		switch (method) {
		case "addStudent":
			addStudent(req,resp);
			break;
		case "deleteStudent":
			deleteStudent(req,resp);
			break;
		case "toUpdate":
			toUpdate(req,resp);
			break;
		case "update":
			update(req,resp);
			break;
		case "pageList":
			pageList(req,resp);
			break;
		case "findAllBanJi":
			findAllBanJi(req,resp);
			break;
		default:
			break;
		}
	}

	private void findAllBanJi(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		List<BanJi> list = studentService.findAllBanJi();
		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().write(jsonArray.toString());
	}

	private void addStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String gender = req.getParameter("gender");
		String banjiId = req.getParameter("banjiId");
		Student student = new Student( name, Integer.parseInt(age), gender,Integer.parseInt(banjiId));
		boolean result = false;
		try {
			result = studentService.add(student);
		} catch (NameAgainException e) {
			e.printStackTrace();
		}
		if (result) {
			resp.sendRedirect(req.getContextPath() + "/student?method=pageList");
		} else {
			PrintWriter writer = resp.getWriter();
			writer.println("注册失败");
		}
	}
	
	private void deleteStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String id = req.getParameter("id");
		boolean result = false;
		result = studentService.deleteById(Integer.parseInt(id));
		if (result) {
			resp.sendRedirect(req.getContextPath() + "/student?method=pageList");
		}else {
			resp.sendRedirect(req.getContextPath() + "/");
		}
	}
	
	private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String id = req.getParameter("id");
		Student student = studentService.toUpdate(Integer.parseInt(id));
		req.setAttribute("student", student);
		req.getRequestDispatcher("/updatestudent.jsp").forward(req, resp);
	}
	
	private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String gender = req.getParameter("gender");
		boolean result = false;
		Student student = new Student(Integer.parseInt(id), name, Integer.parseInt(age), gender);
		result = studentService.update(student);
		if (result) {
			resp.sendRedirect(req.getContextPath() + "/student?method=pageList");
		} else {
			PrintWriter writer = resp.getWriter();
			writer.println("修改失败");
		}
	}
	
	private void pageList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session = req.getSession();
		User user = (User)session.getAttribute("user");
		if (user == null) {
			resp.sendRedirect(req.getContextPath() + "/login.jsp");
			return;
		}
		String pageNoStu = req.getParameter("pageNo");
		int pageNo = (pageNoStu == null)? 1 : Integer.parseInt(pageNoStu);
		String pageSizeStu = req.getParameter("pageSize");
		int pageSize = (pageSizeStu == null) ? 3 : Integer.parseInt(pageSizeStu);
		PageBean pageBean = studentService.findPageBean(pageNo,pageSize);
		req.setAttribute("pageBean", pageBean);
		req.getRequestDispatcher("/student_page_list.jsp").forward(req, resp);
	}
}
